define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

	//设置弹窗宽高
    //Fast.config.openArea = ['100%', '70%'];
	
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reservation/index' + location.search,
                    add_url: 'reservation/add',
                    edit_url: 'reservation/edit',
                    del_url: 'reservation/del',
                    multi_url: 'reservation/multi',
                    table: 'reservation',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('场馆名称'), operate: 'LIKE'},
                        {field: 'manager', title: __('管理员'), operate: 'LIKE'},
                        {field: 'telephone', title: __('联系电话'), operate: 'LIKE'},
                        {field: 'address', title: __('地址'), operate: false},
                        {field: 'status', title: __('状态'), searchList: {1: __('正常'), 0: __('禁用')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons:[
								{
									name: 'orderlist',
									title: __('预约列表'),
									text:__('预约列表'),
									classname: 'btn btn-xs btn-primary btn-dialog',
									url: 'reservation/orderlist',
								},
								{
									name: 'qrcode',
									title: __('场馆二维码'),
									text:__('场馆二维码'),
									classname: 'btn btn-xs btn-warning btn-dialog',
									url: 'reservation/show_qrcode',
									extend: 'data-area=\'["20%","25%"]\''
								},
								{
									name: 'opentimes',
									title: __('开放时间表'),
									text:__('开放时间表'),
									classname: 'btn btn-xs btn-success btn-addtabs',
									url: 'reservation/opentimes',
								},
							], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
		managers: function(){
			 // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reservation/managers',
                }
            });

            var table = $("#table");
			
			// 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
				showToggle: false,
				showExport: false,
				showColumns: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'email', title: __('Email')},
                        {field: 'status', title: __("Status"), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'logintime', title: __('登录时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'selectmanager',
                                    text: '选择',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-check',
                                    click: function (options, row, button) {
                                        Fast.api.close(row);
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
			
			$(document).on("click", "a.btn-select", function () {
                var datas = Table.api.selecteddata(table);
				if (datas.length <= 0) {
					Toastr.error(__('请至少选择一位管理员'));
                    return;
				}
				var nicknames = [];
				var ids = [];
				$.each(datas, function (index, row) {
					nicknames.push(row.nickname);
					ids.push(row.id);
				});
				
				Fast.api.close({'nickname':nicknames.join(','), 'id':ids.join(',')});
            });
		},
		orderlist: function(){
			 // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reservation/orderlist',
					del_url: 'reservation/orderdel',
                }
            });

            var table = $("#table");
			
			// 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
				showToggle: false,
				showExport: false,
				showColumns: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'opentitle', title: __('开放活动')},
                        {field: 'username', title: __('预约人')},
                        {field: 'telephone', title: __('预约人手机')},
						{field: 'daytime', title: __('进场时间')},
						{field: 'family', title: __('是否携带家属'), searchList: {"1":__('是'),"0":__('否')}, formatter: Table.api.formatter.status},
                        {field: 'usernumber', title: __('预约人次')},
                        {field: 'createtime', title: __('预约时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
						{field: 'completime', title: __('核销时间'), formatter: Table.api.formatter.datetime, operate: false, addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
		},
		qrcode: function(){
			
		},
		opentimes: function(){
			// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'reservation/opentimes?ids='+Config.ids,
					add_url: 'reservation/opentimesadd?ids='+Config.ids,
                    edit_url: 'reservation/opentimesedit',
					del_url: 'reservation/opentimesdel',
                }
            });

            var table = $("#table");
			
			// 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
				showToggle: false,
				maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
						{field: 'reservation.title', title: __('场馆名称')},
                        {field: 'title', title: __('活动名称')},
                        {field: 'date_week', title: __('日期/星期')},
                        {field: 'timezone', title: __('时间段')},
						{field: 'peoplelimit', title: __('活动人数上限')},
						{field: 'sort', title: __('排序')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
		},
		opentimesadd: function () {
			$(document).on("change", "input[name='row[timetype]']", function () {
				if(this.value == 1)
				{
					$('#datetype').show();
					$('#weektype').hide();
				}
				else
				{
					$('#datetype').hide();
					$('#weektype').show();
				}
			});
            Controller.api.bindevent();
        },
		opentimesedit: function () {
			$(document).on("change", "input[name='row[timetype]']", function () {
				if(this.value == 1)
				{
					$('#datetype').show();
					$('#weektype').hide();
				}
				else
				{
					$('#datetype').hide();
					$('#weektype').show();
				}
			});
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
				$(document).on("click", ".btn-select-managers", function () {
                    var url = $(this).data("url");
                    parent.Fast.api.open(url, "选择管理员", {
                        callback: function (data) {
                            $("#c-manager").val(data.nickname);
							$("#c-managerid").val(data.id);
                        }
                    });
                });
				
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
